package nl.sogyo.objectvisualisatie.core;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import nl.sogyo.objectvisualisatie.GUI.JoGLCanvas;
import nl.sogyo.objectvisualisatie.GUI.MenuBar;
import nl.sogyo.objectvisualisatie.GUI.MethodListView;
import nl.sogyo.objectvisualisatie.GUI.ObjectInfoView;
import nl.sogyo.objectvisualisatie.GUI.ObjectListView;
import nl.sogyo.objectvisualisatie.GUI.SourceView;
import nl.sogyo.objectvisualisatie.GUI.ToolBar;


public class GUICreator {
	
	private JFrame frame;
	private MethodListView methodList;
	private ObjectListView objectListView;
	private ObjectInfoView objectInfoView;
	private SourceView sourceView;
	private JoGLCanvas joGLCanvas;
	private JMenuBar menuBar;
	private MenuBar menu;
	private JPanel contentPane;
	private JToolBar toolBar;
	private ToolBar tools;
	
	/**
	 * Constructor
	 */
	public GUICreator()
	{

	}
	
	/**
	 * Create all GUI-components
	 */
	public void createGUIComponents(Model model)
	{
		Dimension minimumJoglSize = new Dimension(700, 350);
		Dimension minimumMethodSize = new Dimension(100, 100);
		menu = new MenuBar();
		menu.initalise(model);
		menuBar = menu.getMenu();
				
		methodList = new MethodListView();
		methodList.initalise(model);
		methodList.setMinimumSize(minimumMethodSize);
		
		objectListView = new ObjectListView();
		objectListView.initalise(model);
		objectListView.setMinimumSize(minimumMethodSize);
		
		joGLCanvas = new JoGLCanvas();
		joGLCanvas.initalise(model);
		joGLCanvas.setMinimumSize(minimumJoglSize);
		
		tools = new ToolBar();
		tools.initialise(model);
		toolBar = tools.getToolbar();
		
		objectInfoView = new ObjectInfoView();
		objectInfoView.initalise(model);
				
		sourceView = new SourceView();
		sourceView.initalise(model);
		sourceView.setSize(800, 150);//.setPreferredSize(new Dimension(800, 150));
		
		model.attach(methodList);
		model.attach(objectListView);
		model.attach(sourceView);
		model.attach(objectInfoView);
		createFrame();
	}
	/**
	 * Create a frame in which all components (Canvas, objectlist, methodlist) will be added
	 */
	private void createFrame()
	{
		BorderLayout borderLayout = new BorderLayout();
		frame = new JFrame("Objectvisualisaties");
		contentPane = new JPanel(new BorderLayout());		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(contentPane);
		contentPane.setLayout(borderLayout);
		setLookAndFeel();
		addComponents();
	}
	
	
	/**
	 * Place all the different views of the application to the contentpane
	 */
	private void addComponents()
	{
		//add menubar and toolbar
		frame.setJMenuBar(menuBar);
		contentPane.add(toolBar, BorderLayout.PAGE_START);
		
		//create two splitpanes for the different componetns
		JSplitPane jsPaneObjectObject = new JSplitPane(JSplitPane.VERTICAL_SPLIT,objectListView, objectInfoView);
		JSplitPane jsPaneObjectMethod = new JSplitPane(JSplitPane.VERTICAL_SPLIT,methodList,jsPaneObjectObject);
		JSplitPane jsPaneCanvasSource = new JSplitPane(JSplitPane.VERTICAL_SPLIT,joGLCanvas, sourceView);
		jsPaneCanvasSource.setDividerLocation(400);
		JSplitPane jsPane = new JSplitPane();
		jsPane.setDividerLocation(800);
		jsPane.add(jsPaneObjectMethod, JSplitPane.RIGHT);
		jsPane.add(jsPaneCanvasSource, JSplitPane.LEFT);
		contentPane.add(jsPane, BorderLayout.CENTER);
		
		methodList.setIgnoreRepaint(true);
		objectListView.setIgnoreRepaint(true);
		sourceView.setIgnoreRepaint(true);
		objectInfoView.setIgnoreRepaint(true);
		menuBar.setIgnoreRepaint(true);
		
		frame.setSize(1200, 700);
		frame.setVisible(true);
	}
	
	/**
	 * Change the look and feel of the application to the default of the OS
	 */
	private void setLookAndFeel()
	{
		String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(frame);
		frame.pack();
	}
}
